package com.pinda.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.pinda.common.CustomIdGenerator;
import com.pinda.work.entity.PdTransportOrderTask;
import com.pinda.mapper.PdTransportOrderTaskMapper;
import com.pinda.service.IPdTransportOrderTaskService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 运单与运输任务关联表 服务实现类
 * </p>
 *
 * @author lc
 * @since 2023-06-14
 */
@Service
public class PdTransportOrderTaskServiceImpl extends ServiceImpl<PdTransportOrderTaskMapper, PdTransportOrderTask> implements IPdTransportOrderTaskService {

    @Resource
    PdTransportOrderTaskMapper orderTaskMapper;

    @Autowired
    private CustomIdGenerator idGenerator;
    @Override
    public ArrayList<PdTransportOrderTask> saveTransportOrderTask(String transportTaskId) {
        QueryWrapper<PdTransportOrderTask> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("transport_task_id",transportTaskId);
        ArrayList<PdTransportOrderTask> list = (ArrayList<PdTransportOrderTask>) orderTaskMapper.selectList(queryWrapper);
        return list;
    }

    @Override
    public void batchSaveTransportOrder(List<PdTransportOrderTask> transportOrderTaskList) {
        transportOrderTaskList.forEach(transportOrderTask -> transportOrderTask.setId(idGenerator.nextId(transportOrderTask) + ""));
        saveBatch(transportOrderTaskList);
    }

    @Override
    public void del(String transportOrderId, String transportTaskId) {
        boolean isDel = false;
        LambdaQueryWrapper<PdTransportOrderTask> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        if (StringUtils.isNotEmpty(transportOrderId)) {
            lambdaQueryWrapper.eq(PdTransportOrderTask::getTransportOrderId, transportOrderId);
            isDel = true;
        }
        if (StringUtils.isNotEmpty(transportTaskId)) {
            lambdaQueryWrapper.eq(PdTransportOrderTask::getTransportTaskId, transportTaskId);
            isDel = true;
        }
        if (isDel) {
            remove(lambdaQueryWrapper);
        }
    }
}
